package com.czl.module_pay.viewmodel

import androidx.databinding.ObservableFloat
import androidx.databinding.ObservableInt
import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.PayInfoBean
import com.czl.base.data.bean.WorkOrderPayBean
import com.czl.base.data.bean.tengyun.HouseOrderPayBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper
import com.google.gson.JsonArray


class PayViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    val uc = UiChangeEvent()
    val payAmount = ObservableFloat(0f)
    var payType = ObservableInt(0)
    var isWorkOrder = ""
    var isHouseOrder = ""
    lateinit var paramsMap: Map<String, String>

    class UiChangeEvent {
        val openAliPayEvent: SingleLiveEvent<WorkOrderPayBean> = SingleLiveEvent()
        val openWxPayEvent: SingleLiveEvent<Void> = SingleLiveEvent()
        val houseAliPayEvent: SingleLiveEvent<HouseOrderPayBean> = SingleLiveEvent()
    }

    val btnPayClick: BindingCommand<Void> = BindingCommand(BindingAction {
        placeAnOrder()
    })
    val onWxClick: BindingCommand<Void> = BindingCommand(BindingAction {
        payType.set(0)
    })
    val onAliClick: BindingCommand<Void> = BindingCommand(BindingAction {
        payType.set(1)
    })

    fun setPayParams(paramsMap: Map<String, String>) {
        this.paramsMap = paramsMap
        payAmount.set(paramsMap.getValue("payAmount").toFloat())
    }

    private fun placeAnOrder() {

        when (payType.get()) {
            0 -> {
                uc.openWxPayEvent.call()
            }
            1 -> {
                if (isHouseOrder != "") {
                    // 物业缴费
                    val billId = paramsMap["billId"]
                    val billIds = JsonArray()
                    billIds.add(billId)
                    var payInfo = GsonUtils.toJson(
                        mapOf(
                            "billIds" to billIds,
                            "payMethod" to "chdZFB",
                            "ownerId" to paramsMap["ownerId"],
                            "phone" to paramsMap["phone"],
                            "areaId" to paramsMap["areaId"],
                            "payAmount" to paramsMap["payAmount"],
                            "factRegister" to paramsMap["factRegister"],
                            "balaState" to paramsMap["balaState"]
                        )
                    )
                    model.payChdBill(
                        payInfo
                    ).compose(RxThreadHelper.rxSchedulerHelper(this))
                        .doOnSubscribe { showLoading() }
                        .subscribe(object : ApiSubscriberHelper<BaseBean<HouseOrderPayBean>>() {
                            override fun onResult(t: BaseBean<HouseOrderPayBean>) {
                                dismissLoading()
                                if (t.errorCode == 0) {
                                    if (t.data?.retcode == "SUCCESS") {
                                        uc.houseAliPayEvent.postValue(t.data)
                                    } else {
                                        showErrorToast("支付失败")
                                    }
                                } else {
                                    showErrorToast(t.msg)
                                }
                            }

                            override fun onFailed(msg: String?) {
                                dismissLoading()
                                showErrorToast(msg)
                            }

                        })
                }
                else if (isWorkOrder != "") {
                    //工单下单
                    model.payWorkOrder(
                        paramsMap + mapOf("payType" to "ZFB_SM")
                    ).compose(RxThreadHelper.rxSchedulerHelper(this))
                        .doOnSubscribe { showLoading() }
                        .subscribe(object : ApiSubscriberHelper<BaseBean<WorkOrderPayBean>>() {
                            override fun onResult(t: BaseBean<WorkOrderPayBean>) {
                                dismissLoading()
                                if (t.code == 200) {
                                    if (t.data?.retcode == "SUCCESS") {
                                        uc.openAliPayEvent.postValue(t.data)
                                    } else {
                                        showErrorToast("支付失败")
                                    }
                                } else {
                                    showErrorToast(t.msg)
                                }
                            }

                            override fun onFailed(msg: String?) {
                                dismissLoading()
                                showErrorToast(msg)
                            }

                        })
                }
                    //月卡，临停下单
                    else {
                        //"paymentType" to "1",//1:支付宝 2:微信小程序
                        model.placeAnOrder(
                            paramsMap + mapOf("paymentType" to "1", "sceneType" to "AND_SDK")
                        ).compose(RxThreadHelper.rxSchedulerHelper(this))
                            .doOnSubscribe { showLoading() }
                            .subscribe(object : ApiSubscriberHelper<BaseBean<PayInfoBean>>() {
                                override fun onResult(t: BaseBean<PayInfoBean>) {
                                    dismissLoading()
                                    if (t.code == 200) {
                                        if (t.data?.code == 200) {
                                            uc.openAliPayEvent.postValue(t.data?.data)
                                        } else {
                                            showErrorToast(t.data?.msg)
                                        }

                                    } else {
                                        showErrorToast(t.msg)
                                    }
                                }

                                override fun onFailed(msg: String?) {
                                    dismissLoading()
                                    showErrorToast(msg)
                                }

                            })
                    }


            }
        }
    }


}